<?php

namespace App\Http\Controllers;

use App\Classify;
use App\EnAboutUs;
use App\EnApplicationField;
use App\EnBanner;
use App\EnFooterMenu;
use App\EnGoodsProduct;
use App\EnHumanResources;
use App\EnNews;
use App\EnSysInfo;
use App\Http\Services\FileManager;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class EnHomeController extends Controller
{
    protected $enWebInfo    = null;//统一数据输出
    protected $title      = null;
    protected $enFooterMenu = null;//底部主要内容

    /**
     * 设置头部信息
     * @param $title
     */
    public function setTitle($title){
        $this->title = $title;
    }

    /**
     * 获取头部信息
     * @return null
     */
    public function getTitle(){
        return $this->title;
    }

    public function __construct(){
        $this->getEnWebInfo();
    }

    public function getEnWebInfo(){
        $webInfoArr = null;

        /** @var EnSysInfo $webInfo */
        $allEnWebInfo = EnSysInfo::all();
        if(count($allEnWebInfo) > 0){
            foreach($allEnWebInfo as $webInfo){
                switch($webInfo->type){
                    case 'title':
                        $webInfoArr['title'] = $webInfo->content;
                        break;
                    case 'logo':
                        $webInfoArr['logo_url'] = FileManager::getImgUrl((int)$webInfo->content);
                        break;
                    case 'url':
                        $webInfoArr['url'] = $webInfo->content;
                        break;
                    case 'footer':
                        $webInfoArr['footer_info'] = $webInfo->content;
                        break;
                }
            }
        }

        if(!is_null($this->getTitle())){
            $webInfoArr['title'] = $this->getTitle();
        }

        $bannerArr = array();
        /** @var EnBanner $banner */
        $allEnBanner = EnBanner::all();
        if(count($allEnBanner) > 0){
            foreach($allEnBanner as $banner){
                $bannerUrl = FileManager::getImgUrl($banner->img_id);
                array_push($bannerArr,$bannerUrl);
            }
        }

        $webInfoArr['banner'] = $bannerArr;

        $this->enWebInfo = $webInfoArr;

        //TODO 获取底部主要内容
        /** @var EnFooterMenu $footerMenu */
        $allEnFooterMenu = EnFooterMenu::wherePid(0)->get();
        if(count($allEnFooterMenu) > 0){
            foreach($allEnFooterMenu as $footerMenu){
                $childFooterMenu = EnFooterMenu::wherePid($footerMenu->id)->get();
                $footerMenu->child = $childFooterMenu;
            }
        }

        $this->enFooterMenu = $allEnFooterMenu;

    }


    public function enIndex(Request $request){
        $select = 'en_index';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var EnNews $news */
        $enIndexNews = EnNews::whereIsShow(1)->orderBy('created_at','desc')->take(3)->get();
        if(count($enIndexNews) > 0){
            foreach($enIndexNews as $news){
                $news->thumb_img_url = FileManager::getImgUrl($news->thumb_img_id);
            }
        }

        /** @var EnGoodsProduct $product */
        $enIndexProduct = EnGoodsProduct::whereIsShop(1)->whereClassifyId(15)->orderBy('sort','asc')->take(4)->get();
        $indexImg = null;
        if(count($enIndexProduct) > 0){
            foreach($enIndexProduct as $product){
                if($product->sort == 1){
                    $indexImg = FileManager::getImgUrl($product->thumb_id);
                }

                $product->thumb_url = FileManager::getImgUrl($product->thumb_id);
            }
        }

        /** @var EnApplicationField $applicationField */
        $applicationFieldList = EnApplicationField::orderBy('created_at','desc')->take(3)->get();
        if(count($applicationFieldList) > 0){
            foreach($applicationFieldList as $applicationField){
                $applicationField->thumb_img_url = FileManager::getImgUrl($applicationField->thumb_img_id);
                $applicationField->img_url       = FileManager::getImgUrl($applicationField->img_id);
            }
        }

        $enIndexJoin = EnHumanResources::whereIsShow(1)->orderBy('created_at','desc')->first();

        return view('en_index',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'enIndexNews' => $enIndexNews,'enIndexProduct' => $enIndexProduct,'indexImg' => $indexImg,'enIndexJoin' => $enIndexJoin,'applicationFieldList' => $applicationFieldList]);
    }

    public function enApplicationField(Request $request){
        $select = 'huge';
        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $enApplicationField = EnApplicationField::whereId($id)->first();

        return view('en_application_field',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'enApplicationField' => $enApplicationField]);
    }

    public function enAbout(Request $request){
        $select = 'en_about';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $enDefaultClassify */
        $enDefaultClassify = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($enDefaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $enDefaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $enAboutUs = EnAboutUs::whereClassifyId($classifyId)->whereIsShow(1)->first();

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('en_about',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'classifyId' => $classifyId,'enAboutUs' => $enAboutUs,'classifyName' => $classifyName]);
    }

    public function enBook(Request $request){
        $select = 'en_book';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('en_book',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies]);
    }

    public function enContact(Request $request){
        $select = 'en_contact';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('en_contact',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies]);
    }

    public function enFriend(Request $request){
        $select = 'en_friend';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('en_friend',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies]);
    }

    public function enJoin(Request $request){
        $select = 'en_join';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $enDefaultClassify */
        $enDefaultClassify = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($enDefaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $enDefaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $enJoin = EnHumanResources::whereClassifyId($classifyId)->whereIsShow(1)->first();

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('en_join',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'classifyId' => $classifyId,'enJoin' => $enJoin,'classifyName' => $classifyName]);
    }

    public function enNews(Request $request){
        $select = 'en_news';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $enDefaultClassify */
        $enDefaultClassify = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($enDefaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $enDefaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        /** @var EnNews $new */
        $enNews = EnNews::whereClassifyId($classifyId)->whereIsShow(1)->orderBy('created_at','desc')->paginate(9);
        if(count($enNews) > 0){
            foreach($enNews as $new){
                $new->thumb_img_url = FileManager::getImgUrl($new->thumb_img_id);
            }
        }

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('en_news',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'classifyId' => $classifyId,'enNews' => $enNews,'classifyName' => $classifyName]);
    }

    public function enNewsDetails(Request $request){
        $select = 'en_news';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $enDefaultClassify */
        $enDefaultClassify = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($enDefaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $enDefaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $newsId = !is_null($request->get('news_id')) ? $request->get('news_id') : 0;

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        $thisNew = EnNews::whereId($newsId)->whereIsShow(1)->first();

        return view('en_news-details',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'classifyId' => $classifyId,'thisNew' => $thisNew,'classifyName' => $classifyName]);
    }

    public function enProduct(Request $request){
        $select = 'en_product';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $enDefaultClassify */
        $enDefaultClassify = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($enDefaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $enDefaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        /** @var EnGoodsProduct $product */
        $enProducts = EnGoodsProduct::whereClassifyId($classifyId)->whereIsShop(1)->orderBy('sort','asc')->orderBy('created_at','desc')->paginate(10);
        if(count($enProducts) > 0){
            foreach($enProducts as $product){
                $product->thumb_url = FileManager::getImgUrl($product->thumb_id);
            }
        }

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,30,'...','utf-8');
        }else{
            $classifyName = null;
        }

        return view('en_product',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'classifyId' => $classifyId,'enProducts' => $enProducts,'classifyName' => $classifyName]);
    }

    public function enProductDetails(Request $request){
        $select = 'en_product';

        $enProductClassifies = Classify::whereType('en_product')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enJoinClassifies = Classify::whereType('en_join')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enAboutUsClassifies = Classify::whereType('en_about_us')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        $enNewsClassifies = Classify::whereType('en_news')->orderBy('sort','asc')->orderBy('created_at','desc')->get();

        /** @var Classify $enDefaultClassify */
        $enDefaultClassify = Classify::whereType('product')->orderBy('sort','asc')->orderBy('created_at','desc')->first();
        if($enDefaultClassify){
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : $enDefaultClassify->id;
        }else{
            $classifyId = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        }

        $productId = !is_null($request->get('product_id')) ? $request->get('product_id') : 0;

        /** @var Classify $classify */
        $classify = Classify::whereId($classifyId)->first();
        if($classify){
            $classifyName = mb_strimwidth($classify->name,0,20,'...','utf-8');
        }else{
            $classifyName = null;
        }

        $thisProduct = EnGoodsProduct::whereId($productId)->whereIsShop(1)->first();

        return view('en_product_details',['select' => $select,'en_web_info' => $this->enWebInfo,'en_footer_menu' => $this->enFooterMenu,'enAboutUsClassifies' => $enAboutUsClassifies,'enProductClassifies' => $enProductClassifies,'enNewsClassifies' => $enNewsClassifies,'enJoinClassifies' => $enJoinClassifies,'classifyId' => $classifyId,'thisProduct' => $thisProduct,'classifyName' => $classifyName]);
    }
}
